$(function (){
    // 加载商品列表数据并渲染
    $.ajax({
        url: '../data/jmyp.json',
        type: 'get',
        dataType: 'json',
        success: function (data){
            console.log(data);
            var strDom = ''
            $.each(data,function (index,item){
                var lis = `
                    <li class="goods">
                        <img class="jisu" src="../imgs/new_haitao_icon.jpg" alt="">
                        <a data-id="${item.id}" class="jiaru">加入购物车</a>
                        <div class="img">
                            <div class="imgbox">
                                <img src="${item.imgurl}" alt="">
                                <a class="xqy" tiao-id="${item.id}"></a>
                            </div>
                            <div class="haiwai">
                                海外直采 海外价格 闪电发货
                            </div>
                        </div>
                        <div class="text">
                            <p>${item.title}</p>
                        </div>
                        <div class="price">
                            <i>￥</i><span>${item.price}</span>
                        </div>
                    </li>`
                strDom += lis

               
            })
            $('.uls').html(strDom)
        }
        
    }) 
    

    
    

     if(localStorage.getItem('goods')){
        var goodsArr=JSON.parse(localStorage.getItem('goods'))
    }else{
        var goodsArr=[]
    }

    $('.uls').on('click','.jiaru',function(){
        var id=$(this).attr('data-id')
        var flag=false
        $.each(goodsArr,function(index,item){
            if(item.id===id){
                item.num++
                flag=true
                
            }
        })

        if(!flag){
            goodsArr.push({'id':id,'num':1})
        }
        localStorage.setItem('goods',JSON.stringify(goodsArr))
        alert('加入购物车成功！')
    })


    
    

    $('.uls').on('click','.xqy',function(){
        window.open("http://localhost:3001/xiangqing.html")
        var id=$(this).attr('tiao-id')
       // var num = $(this).attr('data-num')
       // console.log(num);
        var flag = false
          $.each(goodsArr,function(index,item){
           item.key="false"
           console.log(item.id);
            if(item.id===id){
                flag = true
             //   item.num++
              //  num = item.num
                item.key = "ture"
            }
        })  
            if (!flag) {
                goodsArr.push({ 'id': id,'key': 'ture','num':1})
            }
            localStorage.setItem('goods', JSON.stringify(goodsArr))
})

})



var righttop=document.getElementById('righttop')
    onscroll=function(){
        top1=document.documentElement.scrollTop
        if(top1>600){
        righttop.style.display='block'
        }else{
            righttop.style.display='none'
        }
    }
righttop.onclick=function(){
    top1=0
}

